package com.fyp.client.ptma.xml;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import com.fyp.client.ptma.model.ApplicationVersion;
import com.fyp.client.ptma.model.Defect;
import com.fyp.client.ptma.model.Downloader;
import com.fyp.client.ptma.model.Installation;
import com.fyp.client.ptma.model.User;

public class XmlBuilder
{
	public static final String prolog = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

	public static StringBuilder getXmlForAuthetication(Downloader d)
	{
		StringBuilder xml = new StringBuilder(100);

		xml.append(prolog);
		xml.append("<downloader>");
		xml.append("<myEmail>" + d.getEmail() + "</myEmail>");
		xml.append("<deviceId>" + d.getDeviceId() + "</deviceId>");
		xml.append("<googleRegId>" + d.getGoogleRegId() + "</googleRegId>");
		xml.append("<deviceOs>" + d.getDeviceOs() + "</deviceOs>");

		xml.append("<owner>");
		xml.append("<email>");
		xml.append(d.getUser().getEmail());
		xml.append("</email>");
		xml.append("</owner>");

		xml.append("</downloader>");

		return xml;

	}

	public static StringBuilder getXmlForApplicatioList(String email,
			String deviceId)
	{
		StringBuilder xml = new StringBuilder(100);

		xml.append(prolog);
		xml.append("<downloader>");
		xml.append("<email>" + email + "</email>");
		xml.append("<deviceId>" + deviceId + "</deviceId>");
		xml.append("</downloader>");

		return xml;
	}

	public static StringBuilder getXmlForUpdateDownloadStatus(
			Installation installation)
	{
		StringBuilder xml = new StringBuilder(100);

		ApplicationVersion version = installation.getAppVersion();

		String nameSpace = version.getVersionPk().getApplication()
				.getNameSpace();
		String strVersion = version.getVersionPk().getVersion();

		xml.append(prolog);
		xml.append("<installation>");

		// installationPk
		xml.append("<installationPk>");
		xml.append("<version>" + strVersion + "</version>");
		xml.append("<nameSpace>" + nameSpace + "</nameSpace>");
		xml.append("</installationPk>");

		xml.append("</installation>");
		return xml;
	}

	public static StringBuilder getXmlForDefectSubmit(Defect defect)
	{
		StringBuilder xml = new StringBuilder(100);
		xml.append(prolog);
		xml.append("<defect>");
		xml.append("<commenterCode>");
		xml.append(Defect.DOWNLOADER);
		xml.append("</commenterCode>");
		xml.append("<comment>");
		xml.append(defect.getComment());
		xml.append("</comment>");
		xml.append("<stringTimeStamp>");
		xml.append(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Timestamp(System.currentTimeMillis())));
		xml.append("</stringTimeStamp>");
		// Belongs To ApplicationVersion
		xml.append("<appVersion>");
		xml.append("<versionPk>");
		xml.append("<application>");

		xml.append("<nameSpace>"
				+ defect.getAppVersion().getVersionPk().getApplication()
						.getNameSpace() + "</nameSpace>");

		xml.append("</application>");
		xml.append("<version>"
				+ defect.getAppVersion().getVersionPk().getVersion()
				+ "</version>");
		xml.append("</versionPk>");

		xml.append("</appVersion>");
		xml.append("</defect>");
		return xml;

	}

}
